package Comp355Final.main;

import java.util.Locale;

import com.google.tts.TextToSpeechBeta;
import com.google.tts.TextToSpeechBeta.OnInitListener;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class GUITextView extends Activity implements OnInitListener{
    /** Called when the activity is first created. */
	private Button mBtn;
	private EditText mText;
	private TextToSpeechBeta mTTS;
	
	private static final String TAG = "TTS Demo";  
	private static final int REQ_TTS_STATUS_CHECK = 0;  
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeechBeta.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
        
        
        LinearLayout tmp_layout = new LinearLayout(this);
        tmp_layout.setOrientation(LinearLayout.VERTICAL);

        mText = new EditText(this);
        mBtn = new Button(this);
        mBtn.setText("OK");
        mText.setText("Nice to meet you");
        tmp_layout.addView(mText);
        tmp_layout.addView(mBtn);
        
       // mText = (EditText)findViewById(R.id.ttstext);
        //mBtn = (Button) findViewById(R.id.ttsbtn);
        mBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String ttsText = mText.getText().toString();
				//Utility.quick_dialog(ttsText, GUITextView.this);
				if(ttsText != "")
				{
					mTTS.speak(ttsText, TextToSpeechBeta.QUEUE_ADD, null);
				}
			}
		});
        setContentView(tmp_layout);
    } 
	@Override
	public void onInit(int status, int version) {
		
		// TODO Auto-generated method stub
		Log.v(TAG, "version = " + String.valueOf(version));
		if(version == -1)
		{
			alertInstallEyesFreeTTSData();
		}
		else
		{
			if(status == TextToSpeechBeta.SUCCESS)
			{
				Log.v(TAG, "success to init tts");
				mTTS.setEngineByPackageNameExtended("com.google.tts");
				int result = mTTS.setLanguage(Locale.ENGLISH);
				
				if(result == TextToSpeechBeta.LANG_MISSING_DATA || result == TextToSpeechBeta.LANG_NOT_SUPPORTED)
			
				{
					Log.v(TAG, "Language is not available");
					mBtn.setEnabled(false);
				}
				else
				{
					mTTS.speak("Nice to meet you, my friend!", TextToSpeechBeta.QUEUE_ADD, null);
					mBtn.setEnabled(true);
				}
			}
			else
			{
				Log.v(TAG, "failed to init tts");
			}
		}
	}

	protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQ_TTS_STATUS_CHECK)
		{
			switch (resultCode) {
			case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_PASS:
			
			{
			
		        mTTS = new TextToSpeechBeta(this, this);
		        Log.v(TAG, "TTS Engine is installed!");  
				
			}
				
				break;
			case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_BAD_DATA:
			
			case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_MISSING_DATA:
			
			case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
				
			{
				
				Log.v(TAG, "Need language stuff:"+resultCode);
				Intent dataIntent = new Intent();
				dataIntent.setAction(TextToSpeechBeta.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(dataIntent);
			}
				break;
			case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_FAIL:
				
			default:
				Log.v(TAG, "Got a failure. TTS apparently not available");
				break;
			}
		}
		else
		{
			
		}
	}
	
	private void alertInstallEyesFreeTTSData()
	{
		Builder alertInstall = new AlertDialog.Builder(this)
			.setTitle("Missing important sounding data")
			.setMessage("Download the important sounding data")
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
			
					String ttsDataUrl = "http://eyes-free.googlecode.com/files/tts_3.1_market.apk";
					Uri ttsDataUri = Uri.parse(ttsDataUrl);
					Intent ttsIntent = new Intent(Intent.ACTION_VIEW, ttsDataUri);
					startActivity(ttsIntent);
				}
			})
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			alertInstall.create().show();
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(mTTS!=null){
			mTTS.shutdown();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(mTTS != null)
		{
			mTTS.stop();
		}
	}
	
}